package edu.ccut.titan.component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRField;

public class TitanDataSource implements JRDataSource {

	private int index = -1;
	
	private List<Map<String, Object>> datas;

	public void setTitanData(List<Map<String, Object>> list) {
		this.datas.clear();
		this.index = -1;
		
		this.datas.addAll(list);
	}

	public TitanDataSource() {
		this.datas = new ArrayList<Map<String, Object>>();
	}

	public boolean next() throws JRException {
		this.index++;
		if (this.index >= this.datas.size()) {
			this.index = -1;
			
			return false;
		}
		
		return true;
	}

	public Object getFieldValue(JRField field) throws JRException {
		Map<String, Object> map = datas.get(index);
		
		if (map != null) {
			return map.get(field.getName());
		}
		
		return null;
	}
}
